extends CharacterBody2D

const SPEED = 300.0
const JUMP_VELOCITY = -400.0
#var heath = 100
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
@onready var anim_player = get_node("AnimationPlayer") as AnimationPlayer
@onready var anim_sprite = get_node("AnimatedSprite2D") as AnimatedSprite2D
func _physics_process(delta):
	# Add the gravity.
	if not is_on_floor():#不在地面时附加重力速度
		velocity.y += gravity * delta
	
	# Handle jump.cc跳跃时 给一个向上的速度
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY
		anim_player.play("Jump")
	if velocity.y > 0:
		anim_player.play("fall")
	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var direction = Input.get_axis("ui_left", "ui_right")
	#print(direction)
	if direction == -1:
		anim_sprite.flip_h = true
	elif direction == 1: 
		anim_sprite.flip_h = false
	if direction:
		velocity.x = direction * SPEED
		if velocity.y == 0:
			anim_player.play("Run")
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
		if velocity.y == 0:
			anim_player.play("Idle")
			
	#print(velocity.y)
	move_and_slide()
	if Game.playerHP <=0:
		queue_free()
		get_tree().change_scene_to_file("res://main.tscn")

